var Monster = Mobs.extend({
	init : function(id,config) {
		this._super(id,config);
		this.isMoving = true;
		this.steps = 0;
		this.maxHealth = this.health;
		this.spritehit = config.spritehit;
		this.attackSpeed = config.attackSpeed;
		this.isAttacked = false;
		this.damage = config.damage;
		this.questId = (typeof config.questId === 'undefined') ? null : config.questId;
		this.questItem = config.questItem || null;
	},
	draw : function(canvas){
		if(this.isMoving) {
			canvas.ctx.drawImage(this.sprite.image, 0+Math.floor(this.frame)*this.w, 0+this.direction*this.h, this.w, this.h, this.x, this.y, this.w, this.h);	
		}
		else {
			canvas.ctx.drawImage(this.spritehit.image, 0+Math.floor(this.frame)*this.w, 0+this.direction*this.h, this.w, this.h, this.x, this.y, this.w, this.h);	
		}
		canvas.ctx.save();
		canvas.ctx.fillStyle = "red";
		canvas.ctx.fillRect(this.x+5,this.y,50*(this.health/this.maxHealth),4);
		canvas.ctx.restore();
		
	},
	updateFrame : function(){
		if(this.isMoving) {
			if(this.frame >= this.sprite.numberOfFrames){
				this.frame = 0;
			}else{
				this.frame += this.speed/10;
			}		
		}
		else {
			if(this.frame >= this.spritehit.numberOfFrames){
				this.frame = 0;
			}else{
				this.frame += this.attackSpeed/10;
			}	
		}
	},
	walk : function(hero,canvas) {
		this.changeDirection();
		var newx=this.x,newy=this.y;
		switch(this.direction) {
		case 0:
			newy-=this.speed;
			break;
		case 1:
			newx-=this.speed;
			break;
		case 2:
			newy+=this.speed;
			break;
		case 3:
			newx+=this.speed;
		break;
		}
		if(newx+this.w-20 > canvas.width || newx < 0|| newy+this.h-20 > canvas.height || newy < 0 ) {
			if(this.direction<2) {
				this.direction+=2;
			}
			else {
				this.direction-=2;
			}
			this.steps=0;
			return;
		}
		this.x = newx;
		this.y = newy;
		this.steps++;
		this.updateFrame();
	},
	
	changeDirection : function() {
		if(this.steps>=100) {
			switch(this.direction) {
			case 0:
				this.direction += 2;
				this.steps = 0;
				break;
			case 1:
				this.direction += 2;
				this.steps = 0;
				break;
			case 2:
				this.direction -=2;
				this.steps = 0;
				break;
			case 3:
				this.direction -=2;
				this.steps = 0;
			break;
			}
		}
	},
	
	update : function(canvas,hero,player,container, dropables, dropContainer) {
		if(this.health<=0) {
			player.score+=15;
			this.kill(container);
			var dropedItem = Game.prototype.selectItem.call(null, this.x, this.y, dropables);
			Game.prototype.dropItem.call(null, dropedItem, dropContainer);
			
			if(hero.takenQuests[this.questId]){
				if(this.questItem){
					var item = new QuestObjects('monsterQuest' + this.id, {x: this.x, y: this.y, type: this.questItem}); //id-то може да е не е уникално трябва да го управя
					Game.prototype.dropItem.call(null, item, dropContainer);
				}else{
					hero.questItems[this.questId]++;
				}
			
			}	
			return;
		}
		this.attack(hero);
		if(this.isMoving){
			this.walk(hero,canvas);
		}
		this.updateFrame();
	},
	
	kill: function(container){
		delete container[this.id];
	},
	
	detectHero : function(distance,hero) {
		if(Math.abs(hero.x-this.x)<distance && Math.abs(hero.y-this.y)<distance) {
			return true;
		}
		return false;
	},
	
	attack : function(hero) {
		//look for attacking range
		if(this.detectHero(32,hero) && hero.health>0) {
			this.isAttacked = false;
			this.isMoving = false;
			if(this.frame >= 3) {
				hero.health -= this.damage - hero.armor/100;
			}
		}
		else {
			this.isMoving = true;
		}
		//look for chasing the hero
		if((this.detectHero(80,hero) && !this.detectHero(32,hero)) || this.isAttacked) {
			if(Math.abs(this.x-hero.x)>40){
				if(this.x>hero.x) {
					this.x-=this.speed;
					this.direction = 1;
				}
				else {
					this.x+=this.speed;
					this.direction = 3;
				}
			}
			if(Math.abs(this.y-hero.y)>40){
				if(this.y>hero.y) {
					this.y-=this.speed;
					this.direction = 0;
				}
				else {
					this.y+=this.speed;
					this.direction = 2;
				}
			}
			
		}
	}
})
